package services.kingdee

import play.api.{Configuration, Logging}
import play.api.libs.json.{JsObject, Json}
import play.api.libs.ws.WSClient
import javax.inject.{Inject, Singleton}
import scala.concurrent.ExecutionContext

@Singleton
class KingdeePostV2 @Inject()(
                               config: Configuration
                             )(implicit override protected val ws: WSClient, ec: ExecutionContext)
  extends Logging with KingdeePostTrait {

  override var kdAppConfig: KingdeeConfig = config.get[KingdeeConfig]("") // test

  def setKdAppConfig(dcid: String) = {
    try {
      this.kdAppConfig = config.get[KingdeeConfig](dcid)
    } catch {
      case exception: Exception => logger.debug("setKdAppConfig error:", exception)
    }
    this
  }

  def signCode(groupCode: String) = groupCode match {
    case "101" => "H"
    case "102" => "E"
    case "103" => "P"
    case "104" => "R"
    case "107" => "Z"
    case "114" => "J"
    case "201" => "L"
    case "202" => "D"
    case _ => "V"
  }

  /**
   *
   * @return Array[(dcid,name)]
   */
  def get_center = {
    //[{"Id": "625e13dbd009cd","Number": "kddcldt","Name": "礼顿测试1"}]
    getDataCenters().map(r =>
      Json.parse(r).as[Array[JsObject]].map(j =>
        (j("Id").as[String], j("Name").as[String])
      )
    )
  }

  def test(group: String) = {
    group + " =?= " + kdAppConfig.dCID
  }
}
